package com.unionftech.common.common.model

import com.google.gson.Gson
import com.unionftech.baselibrary.base.BaseApplication
import com.unionftech.common.base.mvp.impl.BaseModel
import com.unionftech.common.base.mvp.impl.IBaseCallBack
import com.unionftech.common.bean.ApiException
import com.unionftech.common.bean.ResponseResult
import com.unionftech.common.common.GlobalInfoManager
import com.unionftech.common.common.api.CommonApiManager
import com.unionftech.common.common.bean.AppVersionInfo
import com.unionftech.common.common.bean.BannerImageInfo
import com.unionftech.common.common.bean.BannerPosition
import com.unionftech.common.common.bean.CountryInfo
import com.unionftech.common.common.bean.UserKYCFileInfo
import com.unionftech.common.common.bean.PhoneRegexInfo
import com.unionftech.common.common.bean.TradePlatform
import com.unionftech.common.common.bean.SystemUserInfo
import com.unionftech.common.common.bean.UserGroupInfo
import com.unionftech.common.common.bean.request.SendCodeRequest
import com.unionftech.common.common.bean.request.SendEmailCodeRequest
import com.unionftech.common.common.contract.CommonContract
import com.unionftech.common.common.language.DynamicLanguageLoader
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.network.SimpleNetWork
import com.unionftech.common.network.UploadListener
import com.unionftech.common.network.UploadProgressRequestBody
import com.unionftech.common.utils.Utils
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File

class CommonModel : BaseModel(), CommonContract.ICommonModel {
    private val gson by lazy { Gson() }
    override fun getPublicKey(callBack: IBaseCallBack<ResponseResult<String>>) {
        execute {
            val result = request {
                CommonApiManager.getInstance().apiCommon().getPublicKey()
            }
            callBack.onResponse(result)
        }
    }

    override fun queryCountryByIp(callBack: IBaseCallBack<ResponseResult<CountryInfo>>) {
        execute {
            val result = request {
                CommonApiManager.getInstance().apiCommon().queryCountryByIp()
            }
            callBack.onResponse(result)
        }
    }

    override fun execCountryList(callBack: IBaseCallBack<ResponseResult<List<CountryInfo>>>) {
        execute {
            val responseResult = request {
                CommonApiManager.getInstance().apiCommon().requestCountryList()
            }
            callBack.onResponse(responseResult)
        }
    }

    override fun getAllCountryPhoneRegex(callBack: IBaseCallBack<ResponseResult<List<PhoneRegexInfo>>>) {
        execute {
            val result = request {
                CommonApiManager.getInstance().apiCommon().getAllCountryPhoneRegex()
            }
            callBack.onResponse(result)
        }
    }

    override fun execSendCode(
        requestBean: SendCodeRequest,
        callBack: IBaseCallBack<ResponseResult<Any?>>?,
        token: String?,
    ) {
        execute {
            val result = request {
                val body = gson.toJson(requestBean, SendCodeRequest::class.java)
                    .toRequestBody(SimpleNetWork.JSON)
                CommonApiManager.getInstance().apiCommon().sendCode(body, token)
            }
            callBack?.onResponse(result)
        }
    }

    override fun sendCodeRegister(
        requestBean: SendCodeRequest, callBack: IBaseCallBack<ResponseResult<Any?>>?
    ) {
        execute {
            val result = request {
                val body = gson.toJson(requestBean, SendCodeRequest::class.java)
                    .toRequestBody(SimpleNetWork.JSON)
                CommonApiManager.getInstance().apiCommon().sendCodeRegister(body)
            }
            callBack?.onResponse(result)
        }
    }

    override fun execSendEmailCode(
        requestBean: SendEmailCodeRequest, callBack: IBaseCallBack<ResponseResult<Any?>>?
    ) {
        execute {
            val result = request {
                val body = gson.toJson(requestBean, SendEmailCodeRequest::class.java)
                    .toRequestBody(SimpleNetWork.JSON)
                CommonApiManager.getInstance().apiCommon().sendEmailCode(body)
            }
            callBack?.onResponse(result)
        }
    }

    override fun getOuterUserInfo(
        token: String, callBack: IBaseCallBack<ResponseResult<SystemUserInfo>>
    ) {
        execute {
            val result = request {
                CommonApiManager.getInstance().apiCommon().getOuterUserInfo(token)
            }
            callBack.onResponse(result)
        }
    }

    override fun getKycInfo(
        token: String, callBack: IBaseCallBack<ResponseResult<UserKYCFileInfo>>
    ) {
        execute {
            val result = request {
                CommonApiManager.getInstance().apiCommon().getKycInfo(token)
            }
            callBack.onResponse(result)
        }
    }

    override fun checkInvitationCode(
        token: String, code: String, callBack: IBaseCallBack<ResponseResult<Any?>>
    ) {
        execute {
            val result = request {
                CommonApiManager.getInstance().apiCommon().checkInvitationCode(token, code)
            }
            callBack.onResponse(result)
        }
    }

    override fun querySystemDemoPlatform(
        token: String, callBack: IBaseCallBack<ResponseResult<List<TradePlatform>>>
    ) {
        execute {
            val result = request {
                CommonApiManager.getInstance().apiCommon().querySystemDemoPlatform(token)
            }
            callBack.onResponse(result)
        }
    }

    override fun queryPlatform(
        token: String, code: String, callBack: IBaseCallBack<ResponseResult<List<TradePlatform>>>
    ) {
        execute {
            val result = request {
                CommonApiManager.getInstance().apiCommon().queryPlatform(token, code)
            }
            callBack.onResponse(result)
        }
    }

    override fun queryGroupAndCurrency(
        token: String, code: String, callBack: IBaseCallBack<ResponseResult<UserGroupInfo>>
    ) {
        execute {
            val result = request {
                CommonApiManager.getInstance().apiCommon().queryGroupAndCurrency(token, code)
            }
            callBack.onResponse(result)
        }
    }

    override fun queryAccountType(
        token: String, code: String, callBack: IBaseCallBack<ResponseResult<List<Int>>>
    ) {
        execute {
            val result = request {
                CommonApiManager.getInstance().apiCommon().queryAccountType(token, code)
            }
            callBack.onResponse(result)
        }
    }

    override fun queryCurrency(
        token: String,
        code: String,
        platform: String,
        tradeType: String,
        callBack: IBaseCallBack<ResponseResult<List<String>>>
    ) {
        execute {
            val result = request {
                CommonApiManager.getInstance().apiCommon()
                    .queryCurrency(token, code, platform, tradeType)
            }
            callBack.onResponse(result)
        }
    }

    fun upload(
        file: File,
        progressListener: UploadListener,
    ) {
        execute {
            val result = request {
                // 创建请求体
                val requestFile = UploadProgressRequestBody(
                    file, "multipart/form-data", progressListener
                )
                val multipartBody =
                    MultipartBody.Part.createFormData("file", file.name, requestFile)
                CommonApiManager.getInstance().apiCommon().upload(multipartBody)
            }
            if (result.code == ResponseResult.SUCCESS_CODE) {
                result.data?.also { url ->
                    progressListener.onFinish(url)
                }
            } else {
                progressListener.onError(
                    ApiException.build(
                        ApiException.CODE_SELF_HANDLE_ERROR, result.msg
                    )
                )
            }
        }
    }

    override fun getBannerList(
        position: BannerPosition, callBack: IBaseCallBack<ResponseResult<List<BannerImageInfo>>>
    ) {
        execute {
            val result = request {
                val languageCode = DynamicLanguageLoader.getCurLanguageCode()
                CommonApiManager.getInstance().apiCommon()
                    .getBannerList(languageCode, position.position)
            }
            callBack.onResponse(result)
        }
    }

    override fun getUpdateInfo(callBack: IBaseCallBack<ResponseResult<AppVersionInfo>>) {
        execute {
            val result = request {
                val userId = GlobalInfoManager.getInstance().getSystemUserInfo()?.userId ?: ""
                val device = Utils.getPrettyDeviceName()
                val country = ""
                val versionCode = "V" + Utils.getVersionName(BaseApplication.appContext)
                CommonApiManager.getInstance().apiCommon()
                    .getUpdateInfo(
                        2,
                        versionCode,
                        userId.toString(),
                        device,
                        country
                    )
            }
            callBack.onResponse(result)
        }
    }

}